//
//  EntityInfoPanel.swift
//  Demo3
//
//  Created by Bigboss on 2025/3/11.
//
import SwiftUI

struct EntityInfoPanel: View {
    let entityName: String
    
    // 模拟数据
    private var deviceInfo: [DeviceInfo] {
        [
            DeviceInfo(icon: "network", title: "设备类型", value: "网络交换机/Network Switch"),
            DeviceInfo(icon: "powerplug", title: "当前状态", value: "在线 | Online", status: .online),
            DeviceInfo(icon: "ip", title: "IP地址", value: "192.168.10.254"),
            DeviceInfo(icon: "mappin", title: "物理位置", value: "B1F-03机架/Rack 03"),
            DeviceInfo(icon: "doc.text", title: "设备描述", value: "Cisco Catalyst 9500 Series\n48-Port 10G Switch")
        ]
    }
    
    var body: some View {
        VStack(alignment: .leading, spacing: 12) {
            // 指向箭头
            IndicatorTriangle()
                .fill(Color.white.opacity(0.9))
                .frame(width: 20, height: 12)
                .offset(x: 50, y: -10)
            
            // 面板主体
            VStack(alignment: .leading, spacing: 15) {
                // 标题栏
                HStack {
                    Image(systemName: "server.rack")
                        .foregroundColor(.blue)
                    Text(entityName)
                        .font(.system(.title3, design: .rounded, weight: .semibold))
                        .textCase(.uppercase)
                }
                .padding(.bottom, 8)
                
                // 信息条目
                ForEach(deviceInfo) { info in
                    InfoRow(info: info)
                }
            }
            .padding(20)
            .background(
                RoundedRectangle(cornerRadius: 12)
                    .fill(Color.black.opacity(0.85))
                    .shadow(color: .black.opacity(0.3), radius: 10, x: 0, y: 5)
            )
        }
        .foregroundColor(.white)
    }
}

// MARK: - 子组件
private struct IndicatorTriangle: Shape {
    func path(in rect: CGRect) -> Path {
        Path { path in
            path.move(to: CGPoint(x: rect.midX, y: rect.minY))
            path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY))
            path.addLine(to: CGPoint(x: rect.minX, y: rect.maxY))
            path.closeSubpath()
        }
    }
}

private struct DeviceInfo: Identifiable {
    let id = UUID()
    let icon: String
    let title: String
    let value: String
    var status: DeviceStatus = .normal
    
    enum DeviceStatus {
        case online, offline, normal
    }
}

private struct InfoRow: View {
    let info: DeviceInfo
    
    private var statusColor: Color {
        switch info.status {
        case .online: return .green
        case .offline: return .red
        default: return .gray
        }
    }
    
    var body: some View {
        HStack(alignment: .top, spacing: 12) {
            Image(systemName: info.icon)
                .foregroundColor(statusColor)
                .frame(width: 20)
            
            VStack(alignment: .leading, spacing: 4) {
                HStack {
                    Text(info.title)
                        .font(.system(.caption, design: .monospaced))
                        .foregroundColor(.gray)
                    
                    if info.status != .normal {
                        Circle()
                            .fill(statusColor)
                            .frame(width: 8, height: 8)
                    }
                }
                
                Text(info.value)
                    .font(.system(.subheadline, design: .rounded))
                    .lineSpacing(4)
            }
            
            Spacer()
        }
    }
}
